<?php
namespace Admin\Model;
use Think\Model;

class RegionModel extends Model {

    //查出所有省
	public function getChinaProvince(){
        $china = M('region')->where(1)->field('code,name,level')->order('level,code')->select();
        $return = array();
        foreach ($china as $key => $value) {
            if($value['level']==1){
                $return[$value['code']] = $value;
            }
        }
        return $return;
    }
	public function getChinaArea(){

  //   	return $china;
		//查出所有的分类
        $china = M('region')->where($map)->field('code,name,level,pcode,ppcode')->order('level,code')->select();
		//echo "<pre>";
		//print_r($china);die;
        $return = array();
        foreach ($china as $key => $value) {
            if($value['level']==1){
                $return[$value['code']] = $value;
                $return[$value['code']]['area'] = array();
            }
            if($value['level']==2){
                $return[$value['ppcode']]['area'][$value['code']] = $value;
            }
            if($value['level']==3){
                $return[ $value['ppcode'] ]['area'][ $value['pcode'] ]['area'][ $value['code'] ] = $value;
            }
        }
        return $return;
	}

    public function getNewChinaArea(){
        $china = M('region')->where($map)->field('code,name,level,pcode,ppcode')->order('level')->select();

        $return = array();
        foreach ($china as $key => $value) {
            if($value['level']==1){
                $return[$value['code']] = $value;
                $return[$value['code']]['area'] = array();
            }
            if($value['level']==2){
                $return[$value['ppcode']]['area'][$value['code']] = $value;
            }
            if($value['level']==3){
                $return[ $value['ppcode'] ]['area'][ $value['pcode'] ]['area'][ $value['code'] ] = $value;
            }
        }
        return $return;
    }

    public function getProvince($type=1){
        $map['levelid'] = array('eq',0);
        $region = M("region")->field("id,shortname,code")->where($map)->order("level,id")->select();
        if($type==1){
            foreach ($region as $key => $value) {
                $region[$key]['code'] = substr($value['code'],0,6);
            }
        }
        return $region;
    }

    //查当前省下的所有市区县;
    //修改：@Lintao,参数包含多个省，以逗号分隔
    public function getDowntown($strs){
        $map['ppcode'] = array('IN',$strs);
        $china = M('region')->where($map)->field('code,name,level,pcode,ppcode')->order('level,code')->select();

        $return = array();
        foreach ($china as $key => $value) {
            if($value['level']==1){
                $return[$value['code']] = $value;
                $return[$value['code']]['area'] = array();
            }
            if($value['level']==2){
                $return[$value['ppcode']]['area'][$value['code']] = $value;
            }
            if($value['level']==3){
                $return[ $value['ppcode'] ]['area'][ $value['pcode'] ]['area'][ $value['code'] ] = $value;
            }
        }
        return $return;
    }

    //当前省下的所有市
    public function getCity($province){
        $map = array(ppcode=>$province);
        $china = M('region')->where($map)->field('code,name,level,pcode,ppcode')->order('level,code')->select();

        $return = array();
        foreach ($china as $key => $value) {
            if($value['level']==1){
                $return[$value['code']] = $value;
                $return[$value['code']]['area'] = array();
            }
            if($value['level']==2){
                $return[$value['ppcode']]['area'][$value['code']] = $value;
            }
        }
        return $return;
    }
}